package com.dalimao.floateutil.widget;

import com.dalimao.floateutil.util.AttrUtil;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

public class CanvasView extends Component implements Component.DrawTask{
    //内圆颜色
    Color mInnerBackColor = Color.GRAY;
    //外圆颜色
    Color mOuterBackColor = Color.YELLOW;
    int mRadiusWidget = 15;
    int mProgressTextSize = 18;
    //字体颜色
    Color mProgressTextColor = Color.RED;
    //内圆 弧 文字
    Paint mInnerPaint, mOuterPaint, mProgressTextPaint;
    //当前进度
    int mCurrendNum = 0;
    //总进度
    int mNumMax=100;

    public CanvasView(Context context) {
        this(context, null);
    }

    public CanvasView(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public CanvasView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);

        mInnerBackColor = AttrUtil.getColorValue(attrSet,"innerBackColor","#ef5b9c");
        mOuterBackColor = AttrUtil.getColorValue(attrSet,"outerBackColor","#f58f98");
        mProgressTextColor = AttrUtil.getColorValue(attrSet,"progressTextColor","#2a5caa");
        mRadiusWidget = AttrUtil.getDimension(attrSet,"radiusWidget", AttrHelper.vp2px(mRadiusWidget,context));
        mProgressTextSize = AttrUtil.getDimension(attrSet,"progressTextSize",AttrHelper.fp2px(mProgressTextSize,context));
        initPaint();
        addDrawTask(this);
    }

    private void initPaint() {
        //内圆画笔初始化
        mInnerPaint = paintInit();
        mInnerPaint.setColor(mInnerBackColor);
        mInnerPaint.setStrokeWidth(mRadiusWidget);
        mInnerPaint.setStyle(Paint.Style.STROKE_STYLE);
        //弧初始化
        mOuterPaint = paintInit();
        mOuterPaint.setColor(mOuterBackColor);
        mOuterPaint.setStrokeWidth(mRadiusWidget);
        mOuterPaint.setStyle(Paint.Style.STROKE_STYLE);
        //两端是圆角
        mOuterPaint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        //文字画笔初始化
        mProgressTextPaint = paintInit();
        mProgressTextPaint.setTextSize(mProgressTextSize);
        mProgressTextPaint.setColor(mProgressTextColor);
    }


    @Override
    public void onDraw(Component component, Canvas canvas) {

        //圆心
        int center = getWidth() / 2;
        //画圆  x位置 y位置 半径
        canvas.drawCircle(center, center, center - mRadiusWidget / 2, mInnerPaint);

        //画弧 坐标
        //动态计算进度  当前进度除以总进度 计算出比例
        float sweepAngle=(float) mCurrendNum/mNumMax;
        //mRadiusWidget 圆弧的宽度
        RectFloat rectF = new RectFloat(0 + mRadiusWidget / 2, 0 + mRadiusWidget / 2,
                getWidth() - mRadiusWidget / 2, getHeight() - mRadiusWidget / 2);
        //范围 开始角度 结束角度 是否填充
        Arc arc = new Arc();
        //startAngle 开始角度为左边 0 顺时针
        arc.setArc(-90,sweepAngle*360, false);
        canvas.drawArc(rectF, arc, mOuterPaint);

        //画文字
        String progressText = (int)(sweepAngle*100)+"%";
        //获取文字边界
        Rect rect =mProgressTextPaint.getTextBounds(progressText);
        //计算摆放文字的中心点  ，控件宽度的一半减去文字宽度的一半
        int x = getWidth() / 2 - rect.getCenterX();

        Paint.FontMetrics fontMetrics = mProgressTextPaint.getFontMetrics();
        float dy = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        int baseline = (int) (getHeight() / 2 + dy);
        canvas.drawText(mProgressTextPaint,progressText,x,baseline);
    }

    private Paint paintInit() {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        return paint;
    }

    //设置当前进度
    public void setcurrendNum(int currendNum) {
        this.mCurrendNum = currendNum;
        //必须调用进行重绘
        invalidate();
    }

    //设置总进度
    public void setMaxNum(int numMax){
        if(numMax <=0){
            //总进度不能小于零
            return;
        }
        this.mNumMax=numMax;
    }
}
